package edu.ts.crowdfunding.product.web;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import edu.ts.crowdfunding.product.domain.Project;
import edu.ts.crowdfunding.product.service.ProjectService;
import edu.ts.crowdfunding.product.utils.Result;
import edu.ts.crowdfunding.product.vo.ProjectVo;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * @ClassName ProjectController
 * @Description TODO
 * @Author kai.jiang
 * @Date 2025/6/25 8:55
 * @Version 1.0
 */
@RestController
@RequestMapping("/project")
@Tag(name = "众筹项目接口",description = "...")
public class ProjectController {
    @Autowired
    private ProjectService projectService;
    @PostMapping("/project/{pageNum}/{pageSize}")
    @Operation(summary="项目列表",description = "显示项目列表 1:成功  0:失败")
    public Result<Project> list(@PathVariable int pageNum, @PathVariable int pageSize,@RequestBody ProjectVo projectVo)
    {
        System.out.println(projectVo);
       Result result = new Result();
        try {
            PageHelper.startPage(pageNum, pageSize);
            List<Project> list = projectService.listRef(projectVo);
            PageInfo<Project> pageInfo = new PageInfo<Project>(list);
            result.setCode(1);
            result.setData(pageInfo);
        } catch (Exception e) {
            result.setCode(0);
            throw new RuntimeException(e);
        }
        return result;
    }
    @PostMapping("/save")
    @Operation(summary = "新增项目", description = "成功:1 失败:0")
    public Result save(@RequestBody Project project) {
        Result result = new Result();
        try {
            projectService.save(project);
            result.setCode(1);
        } catch (Exception e) {
            result.setCode(0);
            throw new RuntimeException(e);
        }

        return result;
    }
    @DeleteMapping("/delete/{id}")
    @Operation(summary = "删除项目", description = "成功:1 失败:0")
    public Result delete(@PathVariable int id) {
        Result result = new Result();
        try {
            projectService.removeById(id);
            result.setCode(1);
        } catch (Exception e) {
            result.setCode(0);
            throw new RuntimeException(e);
        }

        return result;
    }
    @GetMapping("/get/{id}")
    @Operation(summary = "获取项目信息", description = "成功:1 失败:0")
    public Result get(@PathVariable int id) {
        Result result = new Result();
        try {
        Project project=    projectService.getProjectRef(id);
        result.setData(project);
            result.setCode(1);
        } catch (Exception e) {
            result.setCode(0);
            throw new RuntimeException(e);
        }

        return result;
    }
    @PatchMapping("/update")
    @Operation(summary = "修改项目信息", description = "成功:1 失败:0")
    public Result update(@RequestBody Project project) {
        Result result = new Result();
        try {
            projectService.updateById(project);
            result.setCode(1);
        } catch (Exception e) {
            result.setCode(0);
            throw new RuntimeException(e);
        }
        return result;
    }
    @PostMapping("/list")
    @Operation(summary="项目列表",description = "显示项目列表 1:成功  0:失败")
    public Result<Project> list1()
    {

        Result result = new Result();
        try {

            List<Project> list = projectService.listRef(null);
            PageInfo<Project> pageInfo = new PageInfo<Project>(list);
            result.setCode(1);
            result.setData(pageInfo);
        } catch (Exception e) {
            result.setCode(0);
            throw new RuntimeException(e);
        }
        return result;
    }

}
